package com.googlecode.allinclusive.fw.core.pagination;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Order;

public class Pagination {
	
	public static final int DEFAULT_PAGE_SIZE = 20;
	
	/** 总记录数*/
	private int totalCount;
	/** 每页记录数*/
	private int pageSize;
	/** 当前页数*/
	private int currentPage;
	/** 排序*/
	private List<Order> orders;
	
	public Pagination(int totalCount) {
		this(totalCount, 1, DEFAULT_PAGE_SIZE);
	}
	
	public Pagination(int totalCount, int currentPage, int pageSize) {
		this.totalCount = totalCount;
		this.currentPage = currentPage;
		this.pageSize = pageSize;
	}

	/**
	 * 获得总页数
	 * @return 总页数
	 */
	public int getTotalPage() {
		if (totalCount < 1) {
			return 0;
		}
		
		return (totalCount + 1) / pageSize + 1;
	}

	/**
	 * 获得当前页开始记录数
	 * @return 当前页开始记录数
	 */
	public int getStartIndex() {
		int startIndex = (currentPage - 1) * pageSize;
		if (startIndex > totalCount) {
			startIndex = totalCount;
		}
		return startIndex;
	}
	
	/**
	 * 获得当前页结束记录数
	 * @return 当前页结束记录数
	 */
	public int getEndIndex() {
		int endIndex = currentPage * pageSize - 1;
		if (endIndex > totalCount) {
			endIndex = totalCount;
		}
		return endIndex;		
	}
	
	/**
	 * 排序
	 * @param order
	 */
	public void addOrder(Order order) {
		if (orders == null) {
			orders = new ArrayList<Order>();
		}
		orders.add(order);
	}
	
	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public List<Order> getOrders() {
		return orders;
	}

	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}
}
